/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.isen.web.webservicetemp.controller;

import fr.isen.web.webservicetemp.Entity.Data;
import fr.isen.web.webservicetemp.Service.ApiService;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author isen
 */
@Controller
public class StuffingController {
    @Autowired
    @Qualifier("asyncWorker")
    AsynchronousWorker asyncWorker;
    @Autowired
    ApiService apiservice;
    
    
    @RequestMapping(value="/stuffinghist")
    public void stuffinghist(HttpServletRequest request, HttpServletResponse response) throws IOException{
            try {
                //Get the data from user's form
               int year   = Integer.parseInt((String) request.getParameter("year"));
               String city   = (String) request.getParameter("cityname");          
                asyncWorker.stuffingHist(apiservice,year,city);
                response.sendRedirect("stuffingprogress.jsp");
            } catch (Exception ex) {
                System.out.println(ex.toString());
                request.setAttribute("error", ex.toString());
                response.sendRedirect("error");
            }
            
    }
    
   @RequestMapping(value="/stuffinggeo")
    public void stuffinggeo(HttpServletRequest request, HttpServletResponse response) throws IOException{
            try {
                //Get the data from user's form
               DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
               String strDate = (String) request.getParameter("datepicker");
               Date date = dateFormat.parse(strDate);
               float originlong   = Float.parseFloat((String) request.getParameter("originlong"));
               float originlat   = Float.parseFloat((String) request.getParameter("originlat"));
               float distancelong   = Float.parseFloat((String) request.getParameter("distancelong"));
               float distancelat   = Float.parseFloat((String) request.getParameter("distancelat"));
               float step   = Float.parseFloat((String) request.getParameter("step"));

                asyncWorker.stuffingGeo(apiservice, originlong, originlat, distancelong, distancelat, step, date);
                response.sendRedirect("stuffingprogress.jsp");
            } catch (Exception ex) {
                System.out.println(ex.toString());
                request.setAttribute("error", ex.toString());
                response.sendRedirect("error");
            }
            
    }
   
   @RequestMapping(value="/stuffing")
   public void stuffing(HttpServletRequest request, HttpServletResponse response){
       String choix = request.getParameter("Choix").toString();
       if (choix.equals("stuffingfrance")) {
           try {
               asyncWorker.bourrageAsynchrone3(apiservice);
           } catch (InterruptedException ex) {
               Logger.getLogger(StuffingController.class.getName()).log(Level.SEVERE, null, ex);
           }
       }
       if (choix.equals("stuffinglillehist")) {
           try {
               asyncWorker.bourrageAsynchrone5(apiservice);
           } catch (InterruptedException ex) {
               Logger.getLogger(StuffingController.class.getName()).log(Level.SEVERE, null, ex);
           }
       }
       if (choix.equals("stuffingworld")) {
           try {
               asyncWorker.bourrageAsynchrone4(apiservice);
           } catch (InterruptedException ex) {
               Logger.getLogger(StuffingController.class.getName()).log(Level.SEVERE, null, ex);
           }
       }
        try {
            response.sendRedirect("stuffingprogress.jsp");
        } catch (IOException ex) {
            Logger.getLogger(StuffingController.class.getName()).log(Level.SEVERE, null, ex);
        }
   }
    
    @RequestMapping(value="stuffingprogress")
    public ModelAndView stuffingProgress(HttpServletResponse response) throws IOException{
            //return modele
            ModelAndView stuffingprogress = new ModelAndView("stuffingprogress");
            return stuffingprogress;
    }
    
}
